# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: speps <speps at aur dot archlinux dot org>
# Contributor: Shinlun Hsieh <yngwiexx@yahoo.com.tw>

pkgbase=qmidiarp
pkgname=(
  qmidiarp
  qmidiarp-{lv2,standalone}
)
pkgver=0.7.0
pkgrel=1
pkgdesc="A MIDI arpeggiator, phrase generator and controller LFO for the ALSA sequencer."
arch=(loong64 x86_64)
url="https://sourceforge.net/projects/qmidiarp/"
license=(GPL2)
makedepends=(
  alsa-lib
  gcc-libs
  glibc
  jack
  liblo
  lv2
  qt5-base
  qt5-tools
)
checkdepends=(lv2lint)
source=(https://downloads.sourceforge.net/project/$pkgbase/$pkgbase/$pkgver/$pkgbase-$pkgver.tar.bz2)
sha512sums=('f1dcb9c817bfc9c39a9b120f5716518c3ef825652258d5852f9e228cbc3018202427a19d4b47858c16d43e5d8bbf3712cced68d95f300cc6b98ad76c608b68fd')
b2sums=('43b558d8185f2e798dc17dc58c966daf244f35b11abf5c96220f81210b39dc619daa260ff384a16da35c74584c90e86330597ec6e170bb071cebf977374c5efa')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd $pkgbase-$pkgver
  autoreconf -fiv
}

build() {
  cd $pkgbase-$pkgver
  ./configure --prefix=/usr --enable-qt5
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

check() {
  local _name

  cd $pkgbase-$pkgver
  # NOTE: we are not running tests for lfo and seq variants because they make lv2lint segfault...
  for _name in arp; do
    cp -v "src/.libs/${pkgbase}_$_name"{,_ui}.so "${pkgbase}_$_name.lv2/"
    # disable failing tests: https://sourceforge.net/p/qmidiarp/bugs/33/
    lv2lint -t "UI Symbols" -t "Plugin Symbols" -Mpack -I "${pkgbase}_${_name}.lv2/" "https://git.code.sf.net/p/qmidiarp/$_name"
  done
}

package_qmidiarp() {
  depends=($pkgbase-{lv2,standalone}=$pkgver)

  make DESTDIR="$pkgdir/" install -C $pkgbase-$pkgver

  (
    cd "$pkgdir"
    _pick $pkgbase-lv2 usr/lib
    _pick $pkgbase-standalone usr/bin
    _pick $pkgbase-standalone usr/share
  )
}

package_qmidiarp-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(
    lv2-plugins
    pro-audio
  )
  depends=(
    gcc-libs
    glibc
    lv2-host
    qt5-base
  )

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,ChangeLog,NEWS,README} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_qmidiarp-standalone() {
  pkgdesc+=" - standalone"
  groups=(
    pro-audio
  )
  depends=(
    alsa-lib
    gcc-libs
    glibc
    hicolor-icon-theme
    jack
    liblo
    qt5-base
  )
  optdepends=('new-session-manager: for session management')

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/{AUTHORS,ChangeLog,NEWS,README} -t "$pkgdir/usr/share/doc/$pkgname/"
}
